package com.jkt.rms.validator;



import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.jkt.rms.commons.utils.EqualityUtils;
import com.jkt.rms.commons.utils.StringUtils;
import com.jkt.rms.constants.IConstants;
import com.jkt.rms.vo.UsersVO;

public class UserMaintenanceValidator implements Validator {
	
	@Override
	public boolean supports(Class clazz) {
		return clazz.equals(UsersVO.class);
		
	}
	
	public void validate(Object obj, Errors errors) {
		UsersVO userObj = (UsersVO) obj;
		if(userObj.getValidationType().equals(IConstants.VALIDATE_ADD) ||
				userObj.getValidationType().equals(IConstants.VALIDATE_UPDATE)) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "error.mandatory.field", new String[]{"First Name"});
		if(userObj.getFirstName()!= null && !StringUtils.isAlphabetSingleQuoteAndSpace(userObj.getFirstName())){
			errors.rejectValue("firstName", "error.alpha.field", new String[]{"First Name"}, null);
		}	
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "error.mandatory.field", new String[]{"Last Name"});
		if(userObj.getLastName()!= null && !StringUtils.isAlpha(userObj.getLastName())){
			errors.rejectValue("lastName", "error.alpha.field", new String[]{"Last Name"}, null);
		}	
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "error.mandatory.field", new String[]{"User Name"});
		
		if(userObj.getValidationType().equals(IConstants.VALIDATE_ADD)){
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "error.mandatory.field", new String[]{"Password"});
			if((EqualityUtils.isNotNull(userObj.getPassword()) &&  userObj.getPassword().contains(" "))) {
				errors.rejectValue("password", "error.space", new String[]{"Password"}, null);
			}
			if(EqualityUtils.isNotNull(userObj.getPassword()) 
					&& userObj.getPassword().length()< IConstants.MINIMUM_PASSWORD_LENGTH) {
				errors.rejectValue("password", "error.min.length", new String[]{"Password"}, null);
			}
		}
		
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "error.mandatory.field", new String[]{"Email"});		
		if(EqualityUtils.isNotNull(userObj.getEmail()) && !StringUtils.validate(userObj.getEmail(),IConstants.EMAIL_PATTERN)) {
			errors.rejectValue("email", "error.invalid", new String[]{"Email"}, null);
		}
		if(EqualityUtils.isNotNull(userObj.getRoleName()) && userObj.getRoleName().equals("0")){
			errors.rejectValue("roleName", "error.mandatory.field", new String[]{"Role Name"}, null);
		}
	} else 	if(userObj.getValidationType().equals(IConstants.VALIDATE_SEARCH)) {
			
			if(EqualityUtils.isNull(userObj.getSrchFirstName()) && EqualityUtils.isNull(userObj.getSrchLastName()) 
					&& EqualityUtils.isNull(userObj.getSrchUserName()) && EqualityUtils.isNull(userObj.getSrchRoleName())) {
				if(errors.getErrorCount()!=0)return;
				errors.reject("error.mandatory",new String[]{"Search Criteria"},null);
			}
		}
	}
}
